//
// 🦠 Corona-Warn-App
//

import Foundation
import XCTest
import HealthCertificateToolkit
@testable import ENA

class HealthCertificateRevocationEntriesTests: XCTestCase {
	
	func testRevocationEntriesCalculation() {
		guard let testEntries = testEntries else {
			XCTFail("Could not decode test entries.")
			return
		}
		for testEntry in testEntries {
			let componentsResult = DigitalCovidCertificateAccess().extractCertificateComponents(from: testEntry.barcodeData)
			
			guard case let .success(certificateComponents) = componentsResult else {
				XCTFail("Could not create certificate.")
				return
			}
			
			let revocationEntries = HealthCertificateRevocationEntries(
				certificate: certificateComponents.certificate,
				header: certificateComponents.header,
				signature: certificateComponents.signature,
				algorithm: certificateComponents.algorithm
			)
			
			XCTAssertEqual(revocationEntries.uci, testEntry.expUCI)
			XCTAssertEqual(revocationEntries.countryCodeUCI, testEntry.expCOUNTRYCODEUCI)
			XCTAssertEqual(revocationEntries.signature, testEntry.expSIGNATURE)
		}

	}
	
	struct TestEntry: Decodable {
		let description: String
		let barcodeData: String
		let expUCI: String
		let expCOUNTRYCODEUCI: String
		let expSIGNATURE: String
	}
	
	lazy var testEntries: [TestEntry]? = {
		let decoder = JSONDecoder()
		guard let testData = testDataString.data(using: .utf8),
			  let testEntries: [TestEntry] = try? decoder.decode([TestEntry].self, from: testData) else {
			return nil
		}
		
		return testEntries
	}()
	
	// swiftlint:disable line_length
	let testDataString = """
	[{
		"description": "DE/1.3.0/REC_DE.png",
		"barcodeData": "HC1:6BFOXN%TSMAHN-HBP8NEDVWLWNG8NB/R804Q1RO4.S0PP7/IX95AAHYI5JENILINO4*J8/Y4IGF5JNBPIFXUY+R8L25LECM8+LAPRA9QH%UF*QHPI2W*VWXMSOEYO9TX2%RU H27RU-YNUVIWQIMC00H6VE6FC1S*R H2/OL*KAOGLK73CLONB7 KLBY47Y81FDRGBM*KBY4:EF/HL:OT9NT-V4.RK0SG*UDHS7G1WN-V7WD+W3:Q7V+DOU732WZP3URG5+NTW7+4WQXDZR3Z2WKTDUR7K$F5H0DYD8KG+2W0AG$LOG60M-6CGIVFCVIOE1M KP8EFRGBTGKRB4O/FE1MJ8UZD5CC9T0HJ6T5QNG.8$$0-FHT-H-RI PQVW5/O16%HAT1Z%PXRQXPMHQ1.GOH4P9B97DDY48.5AH9AVU7:GOJSP130%OK6IA.KKE+4Q:GS99:78ARNJ%44$2%35Y.IE.KD+2H0DL$CWT7XEB0VL//ET2ADMG5JD*5ADK45TMN95SWM934HHP4F5G+P%YQ+GOR.OCDSQHPQHPL*OS OFDSODOH+QC75$/60$QG-ROOOQT5G+Q6/R.9A5Z5FT5D75W9AAABG64XB4*FM9 BO5MD UD-OHL5+$N$EVBLR-KC2YNH0RKOPQ2UHAOA8SA1SCVQIZM-N8Y%F4SL+39%PU00QMBU/ORRZL8.SO$21H41RVFIC7008XD70",
		"expUCI": "423bd0a847634ce8dea5dd5e9400f287",
		"expCOUNTRYCODEUCI": "7dfb27c9adf0551f0600585a9641a026",
		"expSIGNATURE": "2698afff2cdbd017bc957b6fa1764ac7"
	  },
	  {
		"description": "DE/1.3.0/VAC_DE.png",
		"barcodeData": "HC1:6BFOXN%TSMAHN-HWWK2RL99TEZP3Z9M52RAD1WG%MPG*IDJ57PA766$VA9IEPUCF/8X*G-O9 WUQRELS4AETO8FB+2M5536DOWCN8T%*400TFMVU1JLXKEV4L%4NS4KCTX*SZK7FQVUXG+TVEHNVGV6AL**IZMVD0H+BNOA7VH7NS4.$S6ZCJKBKC3KD34LT383N:IN1MPF5RBQ746B46O1N646RM93O5RF6$T61R63B0 %P/IE%TE6UG+ZEAT1HQ13W1:O1 SI5K1*TB3:U-1VVS1UU15%HIMI4UUFTIWMA4SISH9WVHPYH9UE*P1NX1JL89V9ON139G%E5/974ITN$K-PSB 8%SQU IQUSW*PW$PBCK L6VOE:OQPAU:IAJ0AZZ0OWCR/CSP4*2DL43U*0CEBQ/GXQFY73ARNNRTSLJZ73ZQJY$BT3E9%TGST:S23J3UQT8/DCCCD$21NDA-D:FLH0JF0JEYI1DLZZL1629HOR%N6GWD/RQ/6J 8VU2NCJ:DUHUTOFQGWMK96CFCI0OU1WBHJ6 0ZTR3:9C VB5G.BBILCH3W3-QZSDHZMH+GXVPDGW4PN/EAPDW000H%J$YH",
		"expUCI": "54a90383b0e792489835caec8befd2a4",
		"expCOUNTRYCODEUCI": "eeabbcbbb1637ddf343458f1f41842da",
		"expSIGNATURE": "b80307723714cb7cd9a653a0bc56c89d"
	  },
	  {
		"description": "LT/1.3.0/VAC-2.png",
		"barcodeData": "HC1:NCFOXNYTSFDHJI8-.O.VV2+G*F0DJIFI1A83I:IW-4%YGC8951MAJV:X9ANU92LCV4*XUA2PSGH.+H$NI4L6N$Q%UG/YL WO*Z7ON1.-LDJ86Q1:24EI4OCRQZRCKR1-RLV47IQRR69A7N95U/38T9TG90OARH9P1J4HGZJK-GGX2MTDCZ.CA+GS2A:H8UDB+.C6JCX0GTH8DC8%JC7I7FIIYIJGDBQMIGZI1-IIZI.EJJ14B2MZ8DC8C:R16RD:XIBEIVG395EV3EVCK09D5WCFVA.QO5VA81K0ECM8CXVDC8C90JK.A96UJBC.P2R9CWZJS4KT%MCCO CLD03+.PR$P*NIC0J*%3/8B-8B0OI-1BYKE7ON0YHRVC9JAC/BKOAWBAE99C/6SVCOKEH-B8FDQQCIQCPTHU2CMCH3W1V1U:ZJ83B/ TU73FC1V+UTS1S+UHS9UM97H98$QP3R-AH-VC.HRI9WG4BYUH+YR-/6B*H3D4%6E4%VITB+4A$ARXKH2$H+-5J2BQU79AKDH1N04*870WI8*IN6FXX1263VASWX64IF/06+ PRF0FXN8AT 6Q+SBQ8OP1QADPYVKRDUK*1C81:C1BLB$%4G5N.PMI05K9K82W.PT7PSQAU0+2L9TA+FB$EYOOCFC5PI M5C+12UNCJ7F53S.67TU2GQSPQDUF-7V12JM*2 83H0CM BBKEC$UL0KDQ3P4DX+DA7D4V73*J2HEW3UWSRY:8O9JS+6X8RP1R0DAZODEJO/C6BVTNWISH5BO7D.EPBNHW5TWT%93-XU-DV$538CP67TWBNH-M-JVNVVEA1H%C%Z33-7EZQD4W*IGEYK982J/GX+EVGB-.9U0UWP6U.ABU5CQF3H0WD3FEU",
		"expUCI": "3d59bc9b9563dcea32836578c9d3b161",
		"expCOUNTRYCODEUCI": "0c32d9ec22eb7167ea0e7a1eb0a21849",
		"expSIGNATURE": "fa6af59289198a146c35710b67cdd415"
	  }]
	"""

}
